package com.org.wareon.main;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.sql.SQLException;
import java.util.ArrayList;

interface Listener
{
	public void update();
}

public class ModelImage {
	private ArrayList<Listener> listeners;
	private BufferedImage image;
	private BufferedImage rouletteTable;
	
	public ModelImage(BufferedImage image) 
	{
		listeners = new ArrayList<Listener>();
		this.image=image;
	}
	public void addListener(Listener l)
	{
		listeners.add(l);
	}
	public void removeListener(Listener l)
	{
		listeners.remove(l);
	}
	private void notifyListeners()
	{
		for(int i = 0; i < listeners.size(); i++)
		{
			Listener l = (Listener) listeners.get(i);
			l.update();
		}
	}
	public BufferedImage getImage(){
		return image;
		
	}
	public BufferedImage getRouletteTable(){
		return rouletteTable;
		
	}
	public void setRouletteTable(int x, int y, int lengthX, int lengthY, double scale) {
		// TODO Auto-generated method stub
		this.rouletteTable=image.getSubimage(x*((int)(1/scale)), y*((int)(1/scale)), lengthX*((int)(1/scale)), lengthY*((int)(1/scale)));
		this.rouletteTable=scaleImage(this.rouletteTable,(1/scale),(1/scale));
	}
	
	public BufferedImage scaleImage(BufferedImage before,double scaleX,double scaleY){
		int w = before.getWidth();
		int h = before.getHeight();
		BufferedImage after = new BufferedImage(w*2, h*2,
				BufferedImage.TYPE_INT_ARGB);
		AffineTransform at = new AffineTransform();
		at.scale(scaleX, scaleY);
		AffineTransformOp scaleOp = new AffineTransformOp(at,
				AffineTransformOp.TYPE_BILINEAR);
		after = scaleOp.filter(before, after);
		return (after);
	}
	
}
